using System;
using System.Collections.Generic;
using System.Text;

namespace Fuzzy
{
    public class TrapezoidFuzzySet:PieceWiseLinearFuzzySet
    {
         public TrapezoidFuzzySet(FuzzyLinguisticVariable fuzzyLinguisticVariable, double[] peakPositions, double[] peakValues)
            : base(fuzzyLinguisticVariable, peakPositions, peakValues)
        {
            if (peakPositions.Length != 4)
                throw new FuzzyException("Number of peaks should be 4.");
             if ((peakValues[1]!=peakValues[2]) || (peakValues[0]!=peakValues[3]) || (peakValues[0]>=peakValues[1]))
                 throw new FuzzyException("It's not trapezoid.");


        }

        public TrapezoidFuzzySet(FuzzyLinguisticVariable fuzzyLinguisticVariable, string name, double[] peakPositions, double[] peakValues)
            : base(fuzzyLinguisticVariable, name, peakPositions, peakValues)
        {
            if (peakPositions.Length != 4)
                throw new FuzzyException("Number of peaks should be 4.");
            if ((peakValues[1] != peakValues[2]) || (peakValues[0] != peakValues[3]) || (peakValues[0] >= peakValues[1]))
                throw new FuzzyException("It's not trapezoid.");
        }

       public override double Membership(double x)
        {
            if ((x <= fuzzyLinguisticVariable.DomainMin) || (x > fuzzyLinguisticVariable.DomainMax))
                return 0.0;
            if ((x <= peakPositions[0]) || (x > peakPositions[3]))
                return peakValues[0];
            if (x <= peakPositions[1])
                return peakValues[0] + (x - peakPositions[0]) * (peakValues[1] - peakValues[0]) / (peakPositions[1] - peakPositions[0]);
            if (x > peakPositions[1] && x <= peakPositions[2])
                return peakValues[1];
            if (x > peakPositions[2])
                return peakValues[2] + (x - peakPositions[2]) * (peakValues[3] - peakValues[2]) / (peakPositions[3] - peakPositions[2]);
            return 0.0;
        }

    }
}
